﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SARUserControls
{
	/// <summary>
	/// 由一个图片组成的按钮
	/// </summary>
	public partial class SARImageButton : UserControl
	{
		public SARImageButton()
		{
			this.InitializeComponent();
		}

        /// <summary>
        /// 图片路径，注意控件的Width和Height和图片的
        /// Width和Height应保持一直
        /// </summary>
        public string SARImageSource
        {
            set
            {
                this.imgBack.Source = new BitmapImage(new Uri(value, UriKind.RelativeOrAbsolute));
            }
            get
            {
                return this.imgBack.Source.ToString();
            }
        }

        /// <summary>
        /// 单击事件
        /// </summary>
        public event MouseButtonEventHandler SARClick;

        private void gridMouseCapture_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.imgBack.Margin = new Thickness(1, 1, 0, 0);
            if (SARClick != null)
            {
                SARClick.Invoke(this, e);
            }
        }

        private void gridMouseCapture_MouseLeave(object sender, MouseEventArgs e)
        {
            this.imgBack.Margin = new Thickness(0, 0, 0, 0);
        }

        private void gridMouseCapture_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.imgBack.Margin = new Thickness(0, 0, 0, 0);
        }

        private void UserControl_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                this.Opacity = 1;
            }
            else
            {
                this.Opacity = 0.5;
            }
        }       
	}
     
}